package com.we823.project.controller.repo;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.google.common.collect.Lists;
import com.jquery.autocomplete.model.Suggestion;
import com.we823.project.common.utils.MaterialUtil;
import com.we823.project.common.web.status.PermissionStatus;
import com.we823.project.controller.AbstractBaseController;
import com.we823.project.core.web.WebConstants;
import com.we823.project.model.repo.CategoryModel;
import com.we823.project.model.repo.MaterialBomModel;
import com.we823.project.model.repo.MaterialModel;
import com.we823.project.repository.service.repo.CategoryService;
import com.we823.project.repository.service.repo.MaterialBomService;
import com.we823.project.repository.service.repo.MaterialService;
import com.we823.utils.web.RequestUtil;

@Controller
@RequestMapping(WebConstants.REPO_MATERIAL)
public class MaterialController extends AbstractBaseController<MaterialModel> {

	private String baseUrl = WebConstants.REPO_MATERIAL_TEMPLATE;

	private MaterialService materialService;

	@Autowired
	private MaterialBomService materialBomService;

	private @Resource CategoryService categoryService;

	@Autowired
	public MaterialController(MaterialService materialService) {
		super(materialService);
		this.materialService = materialService;
		super.baseUrl = baseUrl;
		super.entityName = "material";
		setResourceIdentity("data:material");
	}

	@Override
	public Page<MaterialModel> getData(PageRequest pageRequest,
			HttpServletRequest request, Model model, boolean pageable) {
		Page<MaterialModel> materialPage = null;
		Integer categoryId = RequestUtil.getValueByParam(request,
				"category_id", 0);
		if (categoryId == 0) {
			if (pageable) {
				if (pageRequest != null) {
					materialPage = materialService.findAll(pageRequest);
				}
			} else {
				materialPage = new PageImpl<>(materialService.findAll());
			}
		} else {
			CategoryModel categoryModel = new CategoryModel();
			categoryModel.setId(categoryId);
			materialPage = materialService.findByCategory(categoryModel,
					pageRequest);
		}
		fillInfo(materialPage);
		return materialPage;
	}

	@RequestMapping("init")
	public String init() {
		// check(PermissionStatus.CREATE);
		CategoryModel categoryModel = categoryService.findOne(1);
		List<CategoryModel> categoryModels = Lists.newArrayList(categoryModel);
		List<MaterialModel> materialModels = materialService
				.findByCategoryIn(categoryModels);
		if (materialModels != null) {

			for (MaterialModel materialModel : materialModels) {
				String name = materialModel.getName();
				String model = materialModel.getModel();
				
				String[] colors = MaterialUtil.getColor(model);
				Object[] wires = MaterialUtil.getWire(name);
				
				if (colors!=null) {
					System.out.println(model+" >> "+colors[0]+" >>"+colors[1]);
				}
				if (wires!=null) {
					System.out.println(name+" >>"+wires[0]+" >>"+wires[1]);
				}
			}

		}
		return super.listUrl;
	}

	@RequestMapping("listAutoModel")
	@ResponseBody
	public Model listAutoModel(HttpServletRequest request, Model model) {
		check(PermissionStatus.VIEW);
		String queryString = RequestUtil.getValueByParam(request, "query");
		String action = RequestUtil.getValueByParam(request, "action");
		PageRequest pageRequest = new PageRequest(0, 20);

		Page<MaterialModel> materialPage = null;
		if ("code".equals(action)) {
			materialPage = materialService.findByCodeContaining(queryString,
					pageRequest);

			if (materialPage != null) {
				List<MaterialModel> materialModels = materialPage.getContent();
				List<Suggestion> suggestions = new ArrayList<>();
				for (MaterialModel materialModel : materialModels) {
					suggestions.add(new Suggestion(materialModel.getCode(),
							materialModel.getName() + " "
									+ materialModel.getModel() + " "
									+ materialModel.getSpecification() + " "
									+ materialModel.getColor()));
				}
				model.addAttribute("suggestions", suggestions);
			}

		} else if ("b".equals(action)) {
			materialPage = materialService.findByCodeContaining(queryString,
					pageRequest);

			if (materialPage != null) {
				List<MaterialModel> materialModels = materialPage.getContent();
				List<Suggestion> suggestions = new ArrayList<>();
				for (MaterialModel materialModel : materialModels) {
					String color = materialModel.getColor();
					String suggestion = materialModel.getFullname();
					if (!StringUtils.isEmpty(color)) {
						suggestion += "("+color+")";
					}
					suggestions.add(new Suggestion(materialModel.getCode(),suggestion));
				}
				model.addAttribute("suggestions", suggestions);
			}
		} else {
			materialPage = materialService.findByFullnameContaining(
					queryString, pageRequest);
			if (materialPage != null) {
				List<MaterialModel> materialModels = materialPage.getContent();
				List<Suggestion> suggestions = new ArrayList<>();
				for (MaterialModel materialModel : materialModels) {
					suggestions.add(new Suggestion(materialModel.getFullname(),
							materialModel.getName() + " "
									+ materialModel.getModel() + " "
									+ materialModel.getSpecification() + " "
									+ materialModel.getColor()));
				}
				model.addAttribute("suggestions", suggestions);
			}
		}

		return model;
	}

	@RequestMapping("getOne")
	@ResponseBody
	public Model getOne(HttpServletRequest request, Model model) {
		check(PermissionStatus.VIEW);
		String code = RequestUtil.getValueByParam(request, "code");
		String action = RequestUtil.getValueByParam(request, "action");
		if (!StringUtils.isEmpty(code)) {
			MaterialModel materialModel = materialService.findByCode(code);
			if (materialModel != null) {
				model.addAttribute("material", materialModel);
			}
		}
		if (!StringUtils.isEmpty(action)) {
			if ("hasBom".equals(action)) {
				List<MaterialBomModel> materialBomModels = materialBomService
						.findByCode(code);
				if (materialBomModels == null || materialBomModels.size() == 0) {
					model.addAttribute("hasBom", false);
				} else {
					model.addAttribute("hasBom", true);
					getSubMaterialBom(materialBomModels, model);
				}
			}
		}
		return model;
	}

	private void getSubMaterialBom(List<MaterialBomModel> materialBomModels,
			Model model) {
		if (materialBomModels != null && materialBomModels.size() > 0) {
			List<String> subBomCodeList = new ArrayList<>();
			for (MaterialBomModel materialBomModel : materialBomModels) {
				List<MaterialBomModel> subBomModels = materialBomService
						.findByCode(materialBomModel.getMaterialCode());
				if (subBomModels != null && subBomModels.size() > 0) {
					materialBomModel.setSubMaterialBoms(subBomModels);
					getSubMaterialBom(subBomModels, model);
					subBomCodeList.add(materialBomModel.getMaterialCode());
				} else {
					continue;
				}
			}
			model.addAttribute("hasSubBom", true);
			model.addAttribute("subBomCodeList", subBomCodeList);
		} else {
			model.addAttribute("hasSubBom", false);
		}

	}

	@RequestMapping("check")
	@ResponseBody
	public boolean check(HttpServletRequest request) {
		String code = RequestUtil.getValueByParam(request, "code");
		MaterialModel materialModel = materialService.findByCode(code);
		if (materialModel != null) {
			return false;
		}
		return true;
	}

	private void fillInfo(Page<MaterialModel> materialPage) {
		if (materialPage != null) {
			List<MaterialModel> materialModels = materialPage.getContent();
			if (materialModels != null) {
				for (MaterialModel materialModel : materialModels) {
					CategoryModel categoryModel = materialModel.getCategory();
					if (categoryModel != null) {
						materialModel.setCategoryName(categoryModel.getName());
					}
				}
			}
		}

	}

	public MaterialBomService getMaterialBomService() {
		return materialBomService;
	}

	public void setMaterialBomService(MaterialBomService materialBomService) {
		this.materialBomService = materialBomService;
	}

}
